// subpackage/clock-in/add/index.js
var e = require("../../../@babel/runtime/helpers/interopRequireDefault").default,
  t = require("../../../@babel/runtime/helpers/objectSpread2"),
  r = require("../../../@babel/runtime/helpers/regeneratorRuntime"),
  i = require("../../../@babel/runtime/helpers/slicedToArray"),
  n = require("../../../@babel/runtime/helpers/asyncToGenerator"),
  a = require("../../../utils/tools/index"),
  s = require("../../../config/index"),
  u = require("../../../components/base/m-button/index"),
  d = e(require("../behavior"));
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgs: [],
    submitFileList: [],
    submitFileIdList: [],
    id: '',
    info: '',
    checkoutType:1
  },
  behaviors: [s.default],
  /**
   * 生命周期函数--监听页面加载
   */
  addimgs() {
    this.selectComponent("#form").setValues({
      images: this.data.imgs
    })
  },
  preview(e) {
    wx.previewImage({
      urls: this.data.submitFileList,
      current: e.currentTarget.dataset.index
    })
  },
  handleDeleteFile(e) {
    var submitFileList = this.data.submitFileList
    var submitFileIdList = this.data.submitFileIdList
    var imgs = this.data.imgs
    submitFileList.splice(e.currentTarget.dataset.index, 1)
    submitFileIdList.splice(e.currentTarget.dataset.index, 1)
    imgs.splice(e.currentTarget.dataset.index, 1)
    this.setData({
      submitFileList: submitFileList,
      submitFileIdList: submitFileIdList,
      imgs:imgs
    })
    this.selectComponent("#form").setValues({
      images: this.data.imgs
    })
  },
  onLoad(options) {
    if (options.id) {
      this.setData({
        id: options.id,
        engineeringId: options.engineeringId
      })
      this.getProjectDetail()
      this.selectComponent("#form").setValues({
        project: {
          id: options.id,
          name: options.name,
        }
      })
    }
  },
  worked_hour_change(e) {
    if(this.data.checkoutType==2) return false;
    var hour = parseFloat(e.detail.value)
    if (hour.toString() == "NaN") {
      this.selectComponent("#form").setValues({
        excavatorTotalAmount: 0,
        driverTotalAmount: 0
      })
    } else {
      this.selectComponent("#form").setValues({
        excavatorTotalAmount: parseFloat(this.data.info.excavatorRentAmount) * parseFloat(hour),
        driverTotalAmount: parseFloat(this.data.info.driverSalaryAmount) * parseFloat(hour)
      })
    }

  },
  getProjectDetail() {
    $.request['GET/business/engineering/detail/' + this.data.engineeringId]().then(res => {
      res = res[0]
      if (res.code == 20002) {
        this.setData({
          info: res.data,
          checkoutType: res.data.checkoutType,
        })
        this.selectComponent("#form").setValues({
          checkoutType: this.data.info.checkoutTypeStr,
          excavatorAmount: this.data.info.excavatorRentAmount,
          driverAmount: this.data.info.driverSalaryAmount
        })
        if(this.data.checkoutType==2){
          this.selectComponent("#form").setValues({
            excavatorTotalAmount: this.data.info.excavatorRentAmount,
            driverTotalAmount: this.data.info.driverSalaryAmount
          })
        }
      }
    })
  },
  chooseImage() {
    wx.navigateTo({
      url: '/subpackage/common/camera/index',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },
  onSubmit: (0, u.apply)(n(r().mark((function e() {
    var i, n, a, s, u, d, o, c, m, p, h, f, _, v;
    return r().wrap((function (e) {
      switch (e.prev = e.next) {
        case 0:
          return a = this.data, s = a.uu_id, u = a.mode, d = a.useTypeValue, e.next = 11,this.selectComponent("#form").verify({
            images: [{
              required: !0,
              message: "请添加照片"
            }],
            totalDuration: [{
              required:true,
              message: "请填写工作时长"
            }]
          });
        case 6:
          if (o = e.sent) {
            e.next = 9;
            break
          }
          return e.abrupt("return", void $.msg("请选择机械用途"));
        case 9:
          if (o.detail && !((null === (i = o.detail) || void 0 === i ? void 0 : i.length) < 5)) {
            e.next = 11;
            break
          }
          return e.abrupt("return", void $.msg("设备简介必须大于5个字符"));
        case 11:
          o = e.sent, console.log(o), $.request['POST/business/engineering/log/clockOut/' + this.data.id]({
            totalDuration: o.totalDuration,
            finishFileList: o.images.map((function (e) {
              return e.url
            })),
            finishFileIdList: o.images.map((function (e) {
              return e.id
            }))
          }).then(res => {
            res = res[0]
            if (res.code == 20002) {
              wx.showToast({
                icon:'none',
                title: '打卡成功',
              })
              setTimeout(() => {
                wx.navigateBack()
              }, 1000);
            } else {
              wx.showToast({
                icon:'none',
                title: res.msg,
              })
            }
          });
        case 13:
        case "end":
          return e.stop()
      }
    }), e, this)
  })))),

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})